﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DatabaseUserTest.DB;

namespace DatabaseUserTest.Controller
{
    public class RouteCTRL
    {
        private DBRoute dbRoute = new DBRoute();
        private DBDestination dbDest = new DBDestination();
        private DBPlane dbPlane = new DBPlane();
        static DatabaseUserTest.DatabaseContext db = new DatabaseUserTest.DatabaseContext();

        public Route FindRoute(string from, string to)
        {
            Destination fromDestination = dbDest.FindDestinationName(from);
            Destination toDestination = dbDest.FindDestinationName(to);
            if (fromDestination != null && toDestination != null)
            {
                if (dbRoute.FindRoute(fromDestination, toDestination) != null)
                {
                    return dbRoute.FindRoute(fromDestination, toDestination);
                }
                
            }
            return null;
        }

        public Route FindRoute(int routeId)
        {
            return dbRoute.FindRoute(routeId);
        }

        public List<Route> FindAllRoutes()
        {
            return dbRoute.FindAllRoutes();
        }
        public bool AddRoute(string fromDestination, string toDestination, string price, string time)
        {
            Route route = new Route();
            double result;
            float floatresult;
            if (fromDestination != null && toDestination != null && (double.TryParse(price, out result) && result > 0) && (float.TryParse(time, out floatresult) && floatresult > 0))
            {
                Destination from = dbDest.FindDestinationName(fromDestination);
                Destination to = dbDest.FindDestinationName(toDestination);
                route.FromDestination = fromDestination;
                route.ToDestination = toDestination;
                route.Price = result;
                route.Time = floatresult;
                dbRoute.AddRoute(route);
                if (dbRoute.FindRoute(from, to) != null)
                {
                    return true;
                }
                
            }
            return false;

        }
    }
}
